package com.mostka.rpc.client;

import com.google.gwt.json.client.*;

public class WsRpc {

    public static String parseJSONString(JSONObject ___presenter, String name){
        if (___presenter.containsKey(name)){
            if (___presenter.get(name).isString() == null) {
                System.err.println("JSON parse error. properity "+name+" is no a string");
                return null;
            }
            return ___presenter.get(name).isString().stringValue();
        }
        return null;
    }
    public static String parseJSONString(JSONValue object){
            if (object.isString() == null) {
                System.err.println("JSON parse error. properity is no a string");
                return null;
            }
            return object.isString().stringValue();
    }
    public static String parseJSONArray(JSONObject ___presenter, String name){
        if (___presenter.containsKey(name)){
            if (___presenter.get(name).isArray() == null && ___presenter.get(name).isNull()==null) {
                System.err.println("JSON parse error. properity "+name+" is no a array");
                return null;
            }

            return ___presenter.get(name).isString().stringValue();
        }
        return null;
    }
    public static String parseJSONArray(JSONObject object){
            if (object.isArray() == null && object.isNull()==null) {
                System.err.println("JSON parse error. properity is no a array");
                return null;
            }

            return object.isString().stringValue();
    }
    public static double parseJSONDouble(JSONObject ___presenter, String name){
        if (___presenter.containsKey(name)){
            if (___presenter.get(name).isNumber() == null) {
                System.err.println("JSON parse error. properity bbb is no a number");
                return 0;
            }
            return ( ___presenter.get(name).isNumber().doubleValue());
        }
        return 0;
    }
    public static double parseJSONDouble(JSONValue object){
        if (object.isNumber() == null) {
            System.err.println("JSON parse error. properity bbb is no a number");
            return 0;
        }
        return ( object.isNumber().doubleValue());
    }
    public static boolean parseJSONBoolean(JSONValue object){
        if (object.isBoolean() == null) {
            System.err.println("JSON parse error. properity bbb is no a number");
            return false;
        }
        return ( object.isBoolean().booleanValue());
    }
    public static JSONArray parseJSONArrayValues(JSONObject ___presenter, String name){
        if (___presenter.containsKey(name)){
            if (___presenter.get(name).isArray() == null && ___presenter.get(name).isNull()==null) {
                System.err.println("JSON parse error. properity "+name+" is no a array");
                return null;
            }
            return ___presenter.get(name).isArray();
        }
        return null;
    }
    public static JSONArray parseJSONArrayValues(JSONValue object){
            if (object.isArray() == null &&object.isNull()==null) {
                System.err.println("JSON parse error. properity is no a array");
                return null;
            }
            return object.isArray();
    }
	
	public static JSONArray toJSONArray(String[] arr){
  		if (arr == null) return null;
		JSONArray jsonArray = new JSONArray();
		for (int i = 0; i < arr.length; i++) {
			if (arr[i]==null) 
				jsonArray.set(i, null);
			else
				jsonArray.set(i, new JSONString(arr[i]));
		}
  		return jsonArray;
  	}
	public static String[] toJSONString(JSONArray jsonArray){
		if (jsonArray == null) return null;
		String[] val = new String[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			if (jsonArray.get(i).isString()==null)
				val[i]=null;
			else
				val[i]=jsonArray.get(i).isString().stringValue();
		}
		return val;
	}
  	public static  JSONArray toJSONArray(int[] arr){
  		if (arr == null) return null;
  		JSONArray jsonArray = new JSONArray();for (int i = 0; i < arr.length; i++) {
				jsonArray.set(i, new JSONNumber(arr[i]));
		}
  		return jsonArray;
  	}
	public static int[] toJSONint(JSONArray jsonArray){
		if (jsonArray == null) return null;
		int[] val = new int[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			val[i]=(int) jsonArray.get(i).isNumber().doubleValue();
		}
		return val;
	}
	
  	public static  JSONArray toJSONArray(double[] arr){
  		if (arr == null) return null;
  		JSONArray jsonArray = new JSONArray();for (int i = 0; i < arr.length; i++) {
				jsonArray.set(i, new JSONNumber(arr[i]));
		}
  		return jsonArray;
  	}
	public static double[] toJSONdouble(JSONArray jsonArray){
		if (jsonArray == null) return null;
		double[] val = new double[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			val[i]=(double) jsonArray.get(i).isNumber().doubleValue();
		}
		return val;
	}
  	public static  JSONArray toJSONArray(char[] arr){
  		if (arr == null) return null;
  		JSONArray jsonArray = new JSONArray();for (int i = 0; i < arr.length; i++) {
				jsonArray.set(i, new JSONNumber(arr[i]));
		}
  		return jsonArray;
  	}
	public static char[] toJSONchar(JSONArray jsonArray){
		if (jsonArray == null) return null;
		char[] val = new char[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			if (jsonArray.get(i).isNumber() != null){
				val[i]=(char) jsonArray.get(i).isNumber().doubleValue();
			}else if (jsonArray.get(i).isString() != null){
				val[i]=jsonArray.get(i).isString().stringValue().toCharArray()[0];
			}else{
				val[i]=(Character) null;
			}
		}
		return val;
	}
  	public static  JSONArray toJSONArray(long[] arr){
  		if (arr == null) return null;
  		JSONArray jsonArray = new JSONArray();for (int i = 0; i < arr.length; i++) {
				jsonArray.set(i, new JSONNumber(arr[i]));
		}
  		return jsonArray;
  	}
	public static long[] toJSONlong(JSONArray jsonArray){
		if (jsonArray == null) return null;
		long[] val = new long[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			val[i]=(long) jsonArray.get(i).isNumber().doubleValue();
		}
		return val;
	}
  	public static  JSONArray toJSONArray(byte[] arr){
  		if (arr == null) return null;
  		JSONArray jsonArray = new JSONArray();for (int i = 0; i < arr.length; i++) {
				jsonArray.set(i, new JSONNumber(arr[i]));
		}
  		return jsonArray;
  	}
	public static byte[] toJSONbyte(JSONArray jsonArray){
		if (jsonArray == null) return null;
		byte[] val = new byte[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			val[i]=(byte) jsonArray.get(i).isNumber().doubleValue();
		}
		return val;
	}
  	public static  JSONArray toJSONArray(short[] arr){
  		if (arr == null) return null;
  		JSONArray jsonArray = new JSONArray();for (int i = 0; i < arr.length; i++) {
				jsonArray.set(i, new JSONNumber(arr[i]));
		}
  		return jsonArray;
  	}
	public static short[] toJSONshort(JSONArray jsonArray){
		if (jsonArray == null) return null;
		short[] val = new short[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			val[i]=(short) jsonArray.get(i).isNumber().doubleValue();
		}
		return val;
	}
  	public static  JSONArray toJSONArray(boolean[] arr){
  		if (arr == null) return null;
  		JSONArray jsonArray = new JSONArray();for (int i = 0; i < arr.length; i++) {
				jsonArray.set(i, new JSONString(arr[i]+""));
		}
  		return jsonArray;
  	}
	public static boolean[] toJSONboolean(JSONArray jsonArray){
		if (jsonArray == null) return null;
		boolean[] val = new boolean[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			if (jsonArray.get(i).isString()!=null)
				if (jsonArray.get(i).isString().stringValue()!="false")
					val[i]=false;
				else
					val[i]=true;
			
		}
		return val;
	}
}
